package com.lnsf.bookstore.system.role.service.impl;

import com.lnsf.bookstore.system.permission.vo.Permission;
import com.lnsf.bookstore.system.role.dao.RolePermissionMapper;
import com.lnsf.bookstore.system.role.service.RolePermissionService;
import com.lnsf.bookstore.system.role.vo.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class RolePermissionServiceImpl implements RolePermissionService{

    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    @Override
    @Transactional
    public void saveOrUpdate(Role role) {

        if(role.getPermissionSet().size() == 0){
            return;
        }
        //先删除
        rolePermissionMapper.deleteByRoleId(role.getId());

        Map<String, Object> map = new HashMap<>();
        map.put("roleId", role.getId());
        List<Permission> permissionList = new ArrayList<Permission>(role.getPermissionSet());
        map.put("permissionList", permissionList);
        rolePermissionMapper.saveOrUpdate(map);


    }
}
